﻿<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="TarreOA.TarreOABLL" %>
<%@ Import Namespace="TarreOA.TarreOAModels" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
    
    private int did;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["did"] != null)
        {
            this.did = int.Parse(Request["did"]);
            this.FindDiaryInfoAndDataTable();
        }
    }

    protected void FindDiaryInfoAndDataTable()
    {
        UserDiarytbService userDiarytbService = new UserDiarytbService();
        if (did != 0)
        {
            UserDiary userDiary = userDiarytbService.findByDiaryId(did);
            this.lblDiaryTitle.Text = userDiary.DiaryTitle;
            this.lblRigisterTime.Text = userDiary.RegisterTime.ToString();
            this.TextBox1.Text = userDiary.DiaryContent;
            DiaryFileUploadMappingtbService diaryFileUploadMappingtbService = new DiaryFileUploadMappingtbService();
            List<DiaryFileUploadMappingtb> list = diaryFileUploadMappingtbService.findByDiaryId(did);
            if (list != null && list.Count > 0)
            {
                DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                dt.Columns.Add("FileName", typeof(string));
                dt.Columns.Add("FilePath", typeof(string));
                foreach (DiaryFileUploadMappingtb diaryFileUploadMapping in list)
                {
                    DataRow dr = dt.NewRow();
                    dr["FileName"] = diaryFileUploadMapping.FileName;
                    dr["FilePath"] = diaryFileUploadMapping.FilePath.Replace(FindConfigurationPath.instance().DiaryFilePath, "");
                    dt.Rows.Add(dr);
                }
                ds.Tables.Add(dt);
                this.dltAffixFiles.DataSource = ds.Tables[0].DefaultView;
                dltAffixFiles.DataBind();
            }
            ViewState["userDiary"] = userDiary.RegisterTime.Date.ToShortDateString();
        }
    }

    protected void lbtDownLoad_Click(object sender, EventArgs e)
    {
        string filePath = FindConfigurationPath.instance().DiaryFilePath + ((LinkButton)sender).Attributes["filePath"].ToString();
        string fileName = ((LinkButton)sender).Attributes["fileName"].ToString();
        FileInfo fi = new FileInfo(filePath);
        if (fi.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment;fileName=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.ContentType = "application/octet-stream";
            Response.Filter.Close();
            Response.WriteFile(fi.FullName);
            Response.End();
        }
    }

    protected void dltAffixFiles_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            ((LinkButton)e.Item.FindControl("lbtDownLoad")).Attributes.Add("fileName", ((HyperLink)e.Item.FindControl("hlkAffixFile")).Text);
            ((LinkButton)e.Item.FindControl("lbtDownLoad")).Attributes.Add("filePath", ((HtmlInputHidden)e.Item.FindControl("hidFilePath")).Value);
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Response.Redirect("WriteDiary.aspx");
    }

    protected void btnExport_ServerClick(object sender, EventArgs e)
    {
        string prefix = "";
        if (ViewState["userDiary"] != null)
        {
            prefix = ViewState["userDiary"].ToString();
        }
        StringWriter sw = new StringWriter();
        sw.WriteLine(this.TextBox1.Text);
        sw.Close();
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(prefix + "工作计划", Encoding.UTF8) + ".txt");
        Response.ContentType = "application/ms-excel";//定义成 excel 需要让用户自己下载 
        Response.ContentEncoding = Encoding.GetEncoding("gb2312");
        Response.Write(sw);
        Response.End();

    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
    <script type="text/javascript" src="../../js/MyOption.js"></script>
    <link rel="Stylesheet" type="text/css" href="../../Styles/right_style.css" />
    <script type="text/javascript" src="../../js/MyOption.js"></script>
    <style type="text/css">
<!--
html{ scrollbar-base-color: #dbecf8; scrollbar-arrow-color: #2a8ed1; scrollbar-track-color: #bdddf2; scrollbar-3dlight-color: #2886c5; scrollbar-darkshadow-color: #2886c5;
scrollbar-face-color: #dbecf8; scrollbar-shadow-color: #dbecf8;  }
-->
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" class="dataList" align="center">
			<tr class="jsmail_bgcolor">
                <td height="30px" align="center">
                    <font style="font-size: 12px">查看计划 </font>
                </td>
            </tr>
            <tr>
                <td align="left">
                    <asp:Label ID="Label1" runat="server" Height="21px" Text="记录时间：" Width="72px"></asp:Label>
                    <asp:Label ID="lblRigisterTime" runat="server" Height="19px" Width="252px"></asp:Label>
                </td>
            </tr>
            <tr>
                <td style="" align="left">
                    <asp:Label ID="Label2" runat="server" Height="21px" Text="计划标题："
                        Width="72px"></asp:Label>
                    <asp:Label ID="lblDiaryTitle" runat="server" Font-Bold="True"
                         Height="19px" Width="564px"></asp:Label>
                </td>
            </tr>
            <tr>
                <td style="" align="left">
                    <asp:Label ID="Label3" runat="server" Height="21px" Text="计划内容和附件："
                        Width="152px"></asp:Label>
                </td>
            </tr>
            <tr>
                <td style="" valign="top">
                    <asp:DataList ID="dltAffixFiles" runat="server" BackColor="White" BorderColor="White"
                        BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" Height="41px"
                        Width="100%" OnItemDataBound="dltAffixFiles_ItemDataBound">
                        <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
                        <SelectedItemStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
                        <ItemTemplate>
                            <table cellpadding="0" cellspacing="0" width="100%">
                                <tr>
                                    <td>
                                        <asp:HyperLink ID="hlkAffixFile" runat="server" Font-Italic="True"
                                           Text='<%# Eval("FileName") %>' Width="606px"></asp:HyperLink>
                                    </td>
                                    <td width="30px">
                                        <asp:LinkButton ID="lbtDownLoad" runat="server" Font-Italic="True"
                                            OnClick="lbtDownLoad_Click">下载</asp:LinkButton>
                                        <input id="hidFilePath" runat="server" type="hidden" value='<%# Eval("FilePath") %>' />
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                        <ItemStyle BackColor="#EFF5FB"/>
                        <HeaderStyle BackColor="#B1D5EF" Font-Bold="True"/>
                    </asp:DataList>
                    <asp:TextBox ID="TextBox1" runat="server" Height="111px" TextMode="MultiLine" Width="99%"
                        ReadOnly="True"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="center" height="30px">
                    <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="写计划" Width="76px" />
                    <input id="btnExport" style="width: 87px" type="button" value="导出计划" onserverclick="btnExport_ServerClick"
                        runat="server" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
